﻿Public Class SmokingAndDrinkHistory
    Private _year As String
    Private _police As New Police()
    Private _organization As New Organization()
    Private _smoking As Boolean
    Private _smokedmoretenperday As Boolean
    Private _drink As New Drink()
    Private _police_fullname As String
    Private _police_age As Short

    Public Property Year() As String
        Get
            Return _year
        End Get
        Set(ByVal value As String)
            _year = value
        End Set
    End Property

    Public Property Police() As Police
        Get
            Return _police
        End Get
        Set(ByVal value As Police)
            _police = value
        End Set
    End Property

    Public Property Organization() As Organization
        Get
            Return _organization
        End Get
        Set(ByVal value As Organization)
            _organization = value
        End Set
    End Property

    Public Property Smoking() As Boolean
        Get
            Return _smoking
        End Get
        Set(ByVal value As Boolean)
            _smoking = value
        End Set
    End Property

    Public Property Smokedmoretenperday() As Boolean
        Get
            Return _smokedmoretenperday
        End Get
        Set(ByVal value As Boolean)
            _smokedmoretenperday = value
        End Set
    End Property

    Public Property Drink() As Drink
        Get
            Return _drink
        End Get
        Set(ByVal value As Drink)
            _drink = value
        End Set
    End Property

    Public Property Police_fullname() As String
        Get
            Return _police_fullname
        End Get
        Set(ByVal value As String)
            _police_fullname = value
        End Set
    End Property

    Public Property Police_age() As Short
        Get
            Return _police_age
        End Get
        Set(ByVal value As Short)
            _police_age = value
        End Set
    End Property

    Public Function getPoliceSmokingAndDrink(ByVal inOrganizationID As String, ByVal inYear As String) As List(Of SmokingAndDrinkHistory)
        Dim outResult As New List(Of SmokingAndDrinkHistory)
        Dim decryptText As New DateUtilities.Encrypt_Data()
        Dim objConn As New MySql.Data.MySqlClient.MySqlConnection( _
            decryptText.getDecrypt(System.Configuration.ConfigurationManager.ConnectionStrings( _
            "PoliceHealthConnectionString").ToString(), True))
        Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand()
        objCmd.Connection = objConn
        objCmd.CommandType = CommandType.StoredProcedure
        objCmd.CommandText = "proc_SELECT_PoliceSmokeAndDrinkHistory"

        Dim objParam As New MySql.Data.MySqlClient.MySqlParameter("inOrganizationID", inOrganizationID)
        objCmd.Parameters.Add(objParam)

        objParam = New MySql.Data.MySqlClient.MySqlParameter("inYear", inYear)
        objCmd.Parameters.Add(objParam)

        Dim objDa As New MySql.Data.MySqlClient.MySqlDataAdapter(objCmd)
        Dim objDs As New System.Data.DataSet()
        objDa.Fill(objDs, "PoliceSmokingAndDrink")

        For Each objDr As System.Data.DataRow In objDs.Tables("PoliceSmokingAndDrink").Rows
            Dim obj As New SmokingAndDrinkHistory()
            obj.Police.citizen_id = objDr.Item("citizen_id")
            obj.Police_fullname = objDr.Item("full_name")
            obj.Police_age = Utilities.ClearDBNullToMinValue(objDr.Item("age"), TypeCode.Int32)
            obj.Smoking = objDr.Item("smoking")
            obj.Smokedmoretenperday = objDr.Item("smokedmoretenperday")
            obj.Drink.Drink_id = objDr.Item("drink_id")
            obj.Drink.Drink_name_tha = objDr.Item("drink_name_tha")
            obj.Drink.Drink_name_eng = objDr.Item("drink_name_eng")
            outResult.Add(obj)
        Next

        Return outResult
    End Function
    Public Function insertPoliceSmokingAndDrink(ByVal inSmokingAndDrinkHistory As SmokingAndDrinkHistory) As Boolean
        Try
            Dim decryptText As New DateUtilities.Encrypt_Data()
            Dim objConn As New MySql.Data.MySqlClient.MySqlConnection( _
                decryptText.getDecrypt(System.Configuration.ConfigurationManager.ConnectionStrings( _
                "PoliceHealthConnectionString").ToString(), True))
            Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand()
            objCmd.Connection = objConn
            objCmd.CommandType = CommandType.StoredProcedure
            objCmd.CommandText = "proc_INSERT_PoliceSmokeAndDrinkHistory"

            Dim objParam As New MySql.Data.MySqlClient.MySqlParameter("inCitizenID", _
                Utilities.SendToDatabase(Utilities.ConvertCitizenID(inSmokingAndDrinkHistory.Police.citizen_id)))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inOrganizationID", _
                Utilities.SendToDatabase(inSmokingAndDrinkHistory.Police.organization.organization_id))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inOrganizationNameTha", _
                 Utilities.SendToDatabase(inSmokingAndDrinkHistory.Police.organization.organization_name_tha))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inYear", _
                Utilities.SendToDatabase(inSmokingAndDrinkHistory.Year))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inSmoking", _
                Utilities.SendToDatabase(inSmokingAndDrinkHistory.Smoking))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inSmokeMoreTen", _
                Utilities.SendToDatabase(inSmokingAndDrinkHistory.Smokedmoretenperday))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inDrinkID", _
                Utilities.SendToDatabase(inSmokingAndDrinkHistory.Drink.Drink_id))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inCreateUser", _
                Utilities.SendToDatabase(inSmokingAndDrinkHistory.Police.create_user))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inUpdateUser", _
                Utilities.SendToDatabase(inSmokingAndDrinkHistory.Police.update_user))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inUpdateDate", _
                Utilities.ConvertDateTimeFormat(inSmokingAndDrinkHistory.Police.update_date.ToString()))
            objCmd.Parameters.Add(objParam)

            objConn.Open()
            objCmd.ExecuteNonQuery()
            objConn.Close()

            Return True
        Catch ex As Exception
            Return False
        End Try

    End Function

End Class
